/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.order.manger;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.lxk.oms.flow.cancel.CancelOrderContext;
import com.lxk.oms.flow.cancel.FlowCancelOrderNameConst;
import com.lxk.oms.flow.cancel.postcancel.PostCancelOrderContext;
import com.lxk.oms.flow.comment.CommentOrderContext;
import com.lxk.oms.flow.comment.FlowCommentOrderNameConst;
import com.lxk.oms.flow.confirm.ConfirmOrderContext;
import com.lxk.oms.flow.confirm.FlowConfirmOrderNameConst;
import com.lxk.oms.flow.order.FlowOrderNameConst;
import com.lxk.oms.flow.order.OrderContext;
import com.lxk.oms.flow.pay.FlowPayNameConst;
import com.lxk.oms.flow.pay.PayContext;
import com.lxk.oms.flow.send.FlowSendOrderNameConst;
import com.lxk.oms.flow.send.SendOrderContext;
import com.lxk.oms.flow.supplement.FlowSupplementNameConst;
import com.lxk.oms.flow.supplement.PaySupplementContext;
import com.lxk.oms.order.domain.req.*;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ValidationUtils;
import org.springframework.stereotype.Service;

import static com.lxk.oms.flow.order.virtual.FlowVirtualOrderNameConst.VIRTUAL_CREATE_ORDER;

/**
 * 订单总入口
 *
 * @author heyi
 * 2024/8/20
 */
@Service
@AllArgsConstructor
public class OrderManger {

    private final FlowExecutor flowExecutor;


    /**
     * 单个商家虚拟订单创建
     *
     * @param userId
     * @param merId  不传默认0
     * @param req
     */
    public OrderContext.OrderResult createVirtualOrder(Long userId, Long merId, CreateOrderReq req) {
        req.setUserId(userId);
        req.setMerId(ObjectUtil.isNull(merId) ? 1L : merId);
        req.setSaveDb(req.getSaveDb());
        req.setNeedStock(false);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(VIRTUAL_CREATE_ORDER, req, OrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(OrderContext.class).getOrderResult();
    }


    /**
     * 单个商家订单创建
     *
     * @param userId
     * @param merId  不传默认0
     * @param req
     */
    public OrderContext.OrderResult createOrder(Long userId, Long merId, CreateOrderReq req) {
        req.setUserId(userId);
        req.setMerId(ObjectUtil.isNull(merId) ? 1L : merId);
        req.setSaveDb(req.getSaveDb());
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowOrderNameConst.CREATE_ORDER_FLOW_NAME, req, OrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(OrderContext.class).getOrderResult();
    }

    /**
     * 预下单
     *
     * @param userId
     * @param merId  不传默认0
     * @param req
     */
    public OrderContext.OrderResult preCreateOrder(Long userId, Long merId, CreateOrderReq req) {
        req.setSaveDb(false);
        return this.createOrder(userId, merId, req);
    }


    /**
     * 强制支付订单
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public PayContext.PayResult payForceOrder(PayOrderReq req) {
        PayContext context = new PayContext();
        context.setYuForcePay(true);
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowPayNameConst.PAY_ORDER, req, context);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(PayContext.class).getPayResult();
    }


    /**
     * 支付订单
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public PayContext.PayResult payOrder(PayOrderReq req) {
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowPayNameConst.PAY_ORDER, req, PayContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(PayContext.class).getPayResult();
    }


    /**
     * 发货订单
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public SendOrderContext.SendOrderResult sendOrder(SendOrderReq req) {
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowSendOrderNameConst.SEND_ORDER, req, SendOrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(SendOrderContext.class).getResult();
    }

    /**
     * 确认收货/使用
     *
     * @param req
     */
    public ConfirmOrderContext.Result confirmOrder(ConfirmOrderReq req) {
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowConfirmOrderNameConst.CONFIRM_ORDER, req, ConfirmOrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(ConfirmOrderContext.class).getResult();
    }

    /**
     * 评价订单
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public CommentOrderContext.Result commentOrder(CommentOrderReq req) {
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowCommentOrderNameConst.COMMENT_ORDER, req, CommentOrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(CommentOrderContext.class).getResult();
    }


    /**
     * 取消订单
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public CancelOrderContext.Result cancelOrder(CancelOrderReq req) {
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowCancelOrderNameConst.CANCEL_ORDER, req, CancelOrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(CancelOrderContext.class).getResult();
    }

    /**
     * 取消订单-仅售后模块可以使用。
     * 非售后模块,看下这个接口
     * @see com.lxk.oms.order.manger.OrderManger#cancelOrder(com.lxk.oms.order.domain.req.CancelOrderReq)
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public CancelOrderContext.Result cancelOrderFormPostOrder(CancelOrderReq req) {
        ValidationUtils.validate(req);
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowCancelOrderNameConst.POST_ORDER_CANCEL_ORDER, req, PostCancelOrderContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(CancelOrderContext.class).getResult();
    }


    /**
     * 支付订单补单
     * 补单
     *
     * @param supplementReq
     */
    @DSTransactional(rollbackFor = Exception.class)
    public PaySupplementContext.SupplementResult paySupplement(SupplementReq supplementReq) {
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowSupplementNameConst.PAY_SUPPLEMENT_ORDER, supplementReq, PaySupplementContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(PaySupplementContext.class).getSupplementResult();
    }
}
